/**
 * Created by yangliying on 16/7/1.
 */
function jsonFile(callback){
    var fs = api.require('fs');
    var fd = '';
    var jsonarr = [];
    fs.open({
        path: 'widget://res/city.json',
        flags:'read'
    }, function(ret, err){
        if(ret.status){
            fd = ret.fd;
            fs.read({
                fd: fd,
                offset: 0,
                length: 0
            }, function(ret, err){
                if(ret){
                    var json = [];
                    var datas = JSON.parse(ret.data);

                    for (var i = 0; i < datas.length; i++){
                        var cityjson = {};
                        //城市
                        cityjson["name"] = datas[i].name;
                        var citys = datas[i].city;
                        // if(citys.length == 1){
                        //     var subarr = [];
                        //     var subarrs = citys[0].area;
                        //     for(var k = 0; k < subarrs.length; k++){
                        //         var subjson = {};
                        //         subjson["name"] = subarrs[k];
                        //         subarr.push(subjson);
                        //     }
                        //     cityjson["sub"] = subarr;

                        // }else {
                            var cityarr = [];
                            for(var j = 0; j < citys.length; j++){
                                 //地区
                                var areajson = {};
                                areajson["name"] = citys[j].name;
                                var areas = citys[j].area;
                                var areaarr = [];
                                for(var k = 0; k < areas.length; k++){
                                    var townjson = {};
                                    townjson["name"] = areas[k];
                                    areaarr.push(townjson);
                                }
                                areajson["sub"] = areaarr;
                                cityarr.push(areajson);
                            }
                            cityjson["sub"] = cityarr;

                        // }
                        json.push(cityjson);

                    }
                    jsonarr = json;
                    callback(jsonarr);

                }else {
						//alert(JSON.stringify(err));
                }

            });
        }else {
				//alert(JSON.stringify(err));
        }
    });

}